package com.zrwl.poscloud.xo.service.impl;

import com.zrwl.poscloud.base.constants.HomeworkBatchConstants;
import com.zrwl.poscloud.base.constants.HomeworkDetailConstants;
import com.zrwl.poscloud.base.entity.HomeworkBatch;
import com.zrwl.poscloud.base.entity.HomeworkDetail;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.entity.HomeworkSubmit;
import com.zrwl.poscloud.base.entity.SysDictData;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.DateUtils;
import com.zrwl.poscloud.commons.utils.JsonUtils;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import com.zrwl.poscloud.xo.service.*;
import com.zrwl.poscloud.xo.vo.EmployeeVO;
import com.zrwl.poscloud.xo.vo.HomeworkSubmitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.zrwl.poscloud.xo.mapper.HomeworkDetailMapper;
import com.zrwl.poscloud.xo.vo.HomeworkDetailVO;

import javax.annotation.Resource;

/**
 * 作业详情 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-03-17
 */
@Service
public class HomeworkDetailServiceImpl extends SuperServiceImpl<HomeworkDetailMapper, HomeworkDetail> implements HomeworkDetailService {

    @Autowired
    private HomeworkDetailService homeworkDetailService;
    @Autowired
    private HomeworkBatchService homeworkBatchService;
    @Autowired
    private EmployeeService employeeService;
    @Resource
    private HomeworkDetailMapper homeworkDetailMapper;
    @Autowired
    private SysDictDataService sysDictDataService;
    @Autowired
    private HomeworkSubmitService homeworkSubmitService;

    @Override
    public IPage<HomeworkDetailVO> getPageList(HomeworkDetailVO homeworkDetailVO) {
        Page<HomeworkDetail> page = new Page<>();
        page.setCurrent(homeworkDetailVO.getCurrentPage());
        page.setSize(homeworkDetailVO.getPageSize());
        IPage<HomeworkDetailVO> pageList = homeworkDetailMapper.getPageList(page, homeworkDetailVO);
        List<HomeworkDetailVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<HomeworkDetailVO> getAllList(HomeworkDetailVO homeworkDetailVO) {
        List<HomeworkDetailVO> list = homeworkDetailMapper.getAllList(homeworkDetailVO);
        List<SysDictData> subjectTypeList = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("subject_type").get("list")), SysDictData.class);
        List<SysDictData> remindTypeList = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("remind_type").get("list")), SysDictData.class);
        assert subjectTypeList != null;
        assert remindTypeList != null;
        for (HomeworkDetailVO detailVO : list) {
            for (SysDictData data : subjectTypeList) {
                if (detailVO.getBatchType() == 0 && detailVO.getBatchType() == Integer.parseInt(data.getDictValue())) {
                    detailVO.setBatchTypeName("班级作业");
                    break;
                } else if (detailVO.getBatchType() == Integer.parseInt(data.getDictValue())) {
                    detailVO.setBatchTypeName(data.getDictLabel());
                    break;
                }
            }
            for (SysDictData data : remindTypeList) {
                if (detailVO.getRemindType() == Integer.parseInt(data.getDictValue())) {
                    detailVO.setRemindTypeName(data.getDictLabel());
                    break;
                }
            }
            detailVO.setStatusName(HomeworkDetailConstants.getStatusName(detailVO.getStatus()));
        }
        return list;
    }

    @Override
    public String add(HomeworkDetailVO homeworkDetailVO) {
        HomeworkDetail entity = new HomeworkDetail();
        BeanUtils.copyProperties(homeworkDetailVO, entity, SysConf.STATUS);
        entity.insert();
        homeworkDetailVO.setUid(entity.getUid());
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(HomeworkDetailVO homeworkDetailVO) {
        HomeworkDetail entity = homeworkDetailService.getById(homeworkDetailVO.getUid());
        BeanUtils.copyProperties(homeworkDetailVO, entity);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String delete(HomeworkDetailVO homeworkDetailVO) {
        HomeworkDetail entity = homeworkDetailService.getById(homeworkDetailVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String layoutWork(HomeworkDetailVO homeworkDetailVO) {
        HomeworkBatch homeworkBatch = homeworkBatchService.getById(homeworkDetailVO.getBatchUid());
        if (homeworkBatch == null) {
            throw new RuntimeException("作业批次不可为空");
        }
        switch (homeworkBatch.getType()) {
            case HomeworkBatchConstants.type_all_course://全科
            case HomeworkBatchConstants.type_class:
                List<EmployeeVO> list = employeeService.getEmployeeAllListByGrade(homeworkDetailVO.getGradeUid());
                for (EmployeeVO employeeVO : list) {
                    HomeworkDetailVO detailVO = new HomeworkDetailVO();
                    BeanUtils.copyProperties(homeworkDetailVO, detailVO);
                    detailVO.setStudentUid(employeeVO.getUid());
                    detailVO.setAcceptUserUid(employeeVO.getUserUid());
                    detailVO.setRemindType(homeworkDetailVO.getRemindType());
                    homeworkDetailService.add(detailVO);
                }
                break;
            case HomeworkBatchConstants.type_employee:
                HomeworkDetailVO detailVO = new HomeworkDetailVO();
                BeanUtils.copyProperties(homeworkDetailVO, detailVO);
                detailVO.setRemindType(homeworkDetailVO.getRemindType());
                homeworkDetailService.add(detailVO);
                break;
        }
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public List<HomeworkDetailVO> getNotFiledWorkList(HomeworkDetailVO homeworkDetailVO, int[] status) {
        List<HomeworkDetailVO> list = homeworkDetailMapper.getNotFiledWorkList(homeworkDetailVO, status);
        if (list != null && !list.isEmpty()) {
            List<SysDictData> subjectTypeList = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("subject_type").get("list")), SysDictData.class);
            assert subjectTypeList != null;
            for (HomeworkDetailVO detailVO : list) {
                for (SysDictData data : subjectTypeList) {
                    if (detailVO.getSubjectType() == 0 && 0 == Integer.parseInt(data.getDictValue())) {
                        detailVO.setSubjectName("全科");
                        break;
                    } else if (detailVO.getSubjectType() == Integer.parseInt(data.getDictValue())) {
                        detailVO.setSubjectName(data.getDictLabel());
                        break;
                    }
                }
                detailVO.setStatusName(HomeworkDetailConstants.getStatusName(detailVO.getStatus()));
            }
        }
        return list;
    }

    @Override
    public HomeworkDetailVO getWorkDetailInfo(HomeworkDetailVO homeworkDetailVO) {
        HomeworkDetailVO workDetailInfo = homeworkDetailMapper.getWorkDetailInfo(homeworkDetailVO);
        //完成执行次数
        Integer finishCount = homeworkDetailMapper.getFinishCount(homeworkDetailVO);
        workDetailInfo.setFinishCount(finishCount == null ? 0 : finishCount);
        List<SysDictData> subjectTypeList = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("subject_type").get("list")), SysDictData.class);
        List<SysDictData> remindTypeList = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("remind_type").get("list")), SysDictData.class);
        assert subjectTypeList != null;
        assert remindTypeList != null;
        for (SysDictData data : subjectTypeList) {
            if (workDetailInfo.getBatchType() == 0 && workDetailInfo.getBatchType() == Integer.parseInt(data.getDictValue())) {
                workDetailInfo.setBatchTypeName("班级作业");
                break;
            } else if (workDetailInfo.getBatchType() == Integer.parseInt(data.getDictValue())) {
                workDetailInfo.setBatchTypeName(data.getDictLabel());
                break;
            }
        }
        for (SysDictData data : remindTypeList) {
            if (workDetailInfo.getRemindType() == Integer.parseInt(data.getDictValue())) {
                workDetailInfo.setRemindTypeName(data.getDictLabel());
                break;
            }
        }
        workDetailInfo.setStatusName(HomeworkDetailConstants.getStatusName(workDetailInfo.getStatus()));
        return workDetailInfo;
    }

    @Override
    public String editAccept(HomeworkDetailVO homeworkDetailVO) {
        HomeworkDetail homeworkDetail = homeworkDetailService.getById(homeworkDetailVO.getUid());
        HomeworkBatch homeworkBatch = homeworkBatchService.getById(homeworkDetail.getBatchUid());
        if (homeworkBatch.getStatus() == HomeworkBatchConstants.not_release) {
            throw new RuntimeException("该作业未发布");
        }
        if (homeworkDetail.getStatus() == HomeworkDetailConstants.stay_submit) {
            throw new RuntimeException("该作业已接收");
        }
        homeworkDetail.setStatus(HomeworkDetailConstants.stay_submit);
        homeworkDetail.setAcceptUserUid(homeworkDetailVO.getAcceptUserUid());
        homeworkDetail.setAcceptTime(new Date());
        homeworkDetail.updateById();
        int repeatCount = 1;
        HomeworkSubmitVO homeworkSubmitVO = new HomeworkSubmitVO();
        homeworkSubmitVO.setBatchUid(homeworkDetailVO.getBatchUid());
        homeworkSubmitVO.setSubmitUserUid(homeworkDetailVO.getStudentUid());
        homeworkSubmitVO.setDetailUid(homeworkDetail.getUid());
        homeworkSubmitVO.setRepeatCount(repeatCount);
        Date submitBeginTime = DateUtils.strToDateTime(DateUtils.dateTimeToStr2(homeworkDetailVO.getFirstSubmitDate()) + " " + homeworkDetailVO.getBeginTime());
        Date submitEndTime = DateUtils.strToDateTime(DateUtils.dateTimeToStr2(homeworkDetailVO.getFirstSubmitDate()) + " " + homeworkDetailVO.getEndTime());
        homeworkSubmitVO.setSubmitBeginTime(submitBeginTime);
        homeworkSubmitVO.setSubmitEndTime(submitEndTime);
        homeworkSubmitService.add(homeworkSubmitVO);
        repeatCount++;
        if (homeworkDetailVO.getFirstSubmitDate() != null) {
            if (homeworkDetailVO.getRepeatMaxCount() > 0) {
                for (int i = 0; i < homeworkDetailVO.getRepeatMaxCount(); i++) {
                    homeworkSubmitVO = new HomeworkSubmitVO();
                    homeworkSubmitVO.setBatchUid(homeworkDetailVO.getBatchUid());
                    homeworkSubmitVO.setSubmitUserUid(homeworkDetailVO.getStudentUid());
                    homeworkSubmitVO.setDetailUid(homeworkDetail.getUid());
                    homeworkSubmitVO.setRepeatCount(repeatCount);
                    switch (homeworkDetailVO.getRepeatType()) {
                        case 2:
                            //日
                            Date dayDate = DateUtils.addDays(homeworkDetailVO.getFirstSubmitDate(), i + 1);
                            homeworkSubmitVO.setSubmitBeginTime(DateUtils.strToDateTime(DateUtils.dateTimeToStr2(dayDate) + " " + homeworkDetailVO.getBeginTime()));
                            homeworkSubmitVO.setSubmitEndTime(DateUtils.strToDateTime(DateUtils.dateTimeToStr2(dayDate) + " " + homeworkDetailVO.getEndTime()));
                            homeworkSubmitService.add(homeworkSubmitVO);
                            repeatCount++;
                            break;
                        case 3:
                            //周
                            Date weekDate = DateUtils.addWeeks(homeworkDetailVO.getFirstSubmitDate(), i + 1);
                            homeworkSubmitVO.setSubmitBeginTime(DateUtils.strToDateTime(DateUtils.dateTimeToStr2(weekDate) + " " + homeworkDetailVO.getBeginTime()));
                            homeworkSubmitVO.setSubmitEndTime(DateUtils.strToDateTime(DateUtils.dateTimeToStr2(weekDate) + " " + homeworkDetailVO.getEndTime()));
                            homeworkSubmitService.add(homeworkSubmitVO);
                            repeatCount++;
                            break;
                        case 4:
                            //月
                            Date monthDate = DateUtils.addMonths(homeworkDetailVO.getFirstSubmitDate(), i + 1);
                            homeworkSubmitVO.setSubmitBeginTime(DateUtils.strToDateTime(DateUtils.dateTimeToStr2(monthDate) + " " + homeworkDetailVO.getBeginTime()));
                            homeworkSubmitVO.setSubmitEndTime(DateUtils.strToDateTime(DateUtils.dateTimeToStr2(monthDate) + " " + homeworkDetailVO.getEndTime()));
                            homeworkSubmitService.add(homeworkSubmitVO);
                            repeatCount++;
                            break;
                    }
                }
            }
        }
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String addSubmit(HomeworkDetailVO homeworkDetailVO) {
        HomeworkDetail homeworkDetail = homeworkDetailService.getById(homeworkDetailVO.getDetailUid());
        HomeworkBatch homeworkBatch = homeworkBatchService.getById(homeworkDetail.getBatchUid());
        if (homeworkBatch.getStatus() == HomeworkBatchConstants.not_release) {
            throw new RuntimeException("该作业未发布");
        }
        if (homeworkBatch.getSubmitStatus() == 0) {
            throw new RuntimeException("该作业无需提交");
        }
        if (homeworkDetail.getStatus() == HomeworkDetailConstants.stay_receive) {
            throw new RuntimeException("该作业未接收");
        }
//        HomeworkSubmitVO homeworkSubmitVO = homeworkSubmitService.getHomeworkSubmitByBatchAndDetail(homeworkDetail.getBatchUid(), homeworkDetail.getUid());
        HomeworkSubmit homeworkSubmit = homeworkSubmitService.getById(homeworkDetailVO.getSubmitUid());
        homeworkSubmit.setSubmitContent(homeworkDetailVO.getSubmitContent());
        homeworkSubmit.setSubmitFiles(homeworkDetailVO.getSubmitFiles());
        homeworkSubmit.setSubmitUserUid(homeworkDetail.getStudentUid());
        homeworkSubmit.setSubmitTime(new Date());
        homeworkSubmit.setStatus(2);//已编辑
        homeworkSubmit.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public IPage<HomeworkDetailVO> getHomeworkList(HomeworkDetailVO homeworkDetailVO) {
        Page<HomeworkDetail> page = new Page<>();
        page.setCurrent(homeworkDetailVO.getCurrentPage());
        page.setSize(homeworkDetailVO.getPageSize());
        IPage<HomeworkDetailVO> pageList = homeworkDetailMapper.getHomeworkList(page, homeworkDetailVO);
        List<HomeworkDetailVO> list = pageList.getRecords();
        List<SysDictData> subjectTypeList = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("subject_type").get("list")), SysDictData.class);
        List<SysDictData> remindTypeList = JsonUtils.jsonToList(JsonUtils.objectToJson(sysDictDataService.getListByDictType("remind_type").get("list")), SysDictData.class);
        assert subjectTypeList != null;
        assert remindTypeList != null;
        for (HomeworkDetailVO detailVO : list) {
            for (SysDictData data : subjectTypeList) {
                if (detailVO.getBatchType() == 0 && detailVO.getBatchType() == Integer.parseInt(data.getDictValue())) {
                    detailVO.setBatchTypeName("班级作业");
                    break;
                } else if (detailVO.getBatchType() == Integer.parseInt(data.getDictValue())) {
                    detailVO.setBatchTypeName(data.getDictLabel());
                    break;
                }
            }
            for (SysDictData data : remindTypeList) {
                if (detailVO.getRemindType() == Integer.parseInt(data.getDictValue())) {
                    detailVO.setRemindTypeName(data.getDictLabel());
                    break;
                }
            }
            detailVO.setStatusName(HomeworkDetailConstants.getStatusName(detailVO.getStatus()));
        }
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public IPage<HomeworkDetailVO> getWorkRepeatDetailList(HomeworkDetailVO homeworkDetailVO) {
        Page<HomeworkDetail> page = new Page<>();
        page.setCurrent(homeworkDetailVO.getCurrentPage());
        page.setSize(homeworkDetailVO.getPageSize());
        IPage<HomeworkDetailVO> pageList = homeworkDetailMapper.getWorkRepeatDetailList(page, homeworkDetailVO);
        List<HomeworkDetailVO> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public HomeworkDetailVO getSecondExecutionTime(HomeworkDetailVO homeworkDetailVO) {
        return homeworkDetailMapper.getSecondExecutionTime(homeworkDetailVO);
    }
}
